package jannex.mapping;

import java.lang.reflect.Field;

public class MappedField<E> {

    // Java land
    private final Field field;
    private final Class<?> type;
    private final FieldSetter<E> setter;
    private final FieldGetter<E> getter;

    // SQL land
    private final String table;
    private final String label;
    private final int modifiers;

    private final MappedField<?>[] fields;

    public MappedField(Field field, FieldSetter<E> setter, FieldGetter<E> getter,
                       String table, String label, int modifiers, Class<?> type, MappedField<?>[] fields) {

        this.field = field;
        this.setter = setter;
        this.getter = getter;

        this.table = table;
        this.label = label;
        this.modifiers = modifiers;
        this.type = type;

        this.fields = fields;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MappedField that = (MappedField) o;
        return field.equals(that.field);
    }

    @Override
    public int hashCode() {
        return field.hashCode();
    }

    public String getFieldName() {
        return field.getName();
    }

    public int getModifiers() {
        return modifiers;
    }

    public String getLabel() {
        return label;
    }

    public String getTable() {
        return table;
    }

    public Field getField() {
        return field;
    }

    public Class<?> getType() {
        return type;
    }

    public FieldSetter<E> getSetter() {
        return setter;
    }

    public FieldGetter<E> getGetter() {
        return getter;
    }

    public MappedField<?>[] getFields() {
        return fields;
    }

    public boolean isId() {
        return MappingModifiers.isId(modifiers);
    }

    public boolean isUpdatable() {
        return MappingModifiers.isUpdatable(modifiers);
    }

    public boolean isSelectable() {
        return MappingModifiers.isSelectable(modifiers);
    }

    public boolean isInsertable() {
        return MappingModifiers.isInsertable(modifiers);
    }

    public boolean hasFields() {
        return fields != null;
    }
}
